﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection.Metadata.Ecma335;
using System.Text;
using System.Threading.Tasks;

namespace MySeventhCSharp.Modes
{
    /// <summary>
    ///最小二乘法
    /// </summary>
    public class Method
    {
        double[] year = { 1995, 1996, 1997, 1998, 1999, 2000, 2001,
                       2002, 2003, 2004, 2005, 2006, 2007, 2008, 
                       2009, 2010, 2011, 2012, 2013, 2014, 2015, 
                       2016, 2017, 2018, 2019, 2020 };
        double[] GDP = { 734547898224, 863746717507, 961603952954, 1029043097558, 1093997267277, 1211346869600, 1339395718862,
                         1470550015077, 1660287965663, 1955347004965, 2285965892364, 2752131773358, 3550342737009, 4594307032667,
                        5101703073088, 6087163874510, 7551500124197, 8532229986993, 9570406235659, 10475682920597, 11061553079871,
                        11233276536744, 12310409370894, 13894817549380, 14279937500608, 14687673892882};
        double sumyear, sumGDP, ayear, aGDP,a,b;
      
        //求平均年
        public double getayear()
        {  
            int i = 0;             
            foreach (var item in year)
            {                
                sumyear += item;
                i++;
            }
            ayear = sumyear / i;
            return ayear;
        }
        //求平均GDP
        public double getaGDP()
        {
            int i = 0;
            foreach (var item in GDP)
            {
                sumGDP += item;
                i++;
            }
            aGDP = sumGDP / i;
            return aGDP;
        }
        //计算b值
        public double getb(double ayear, double aGDP)
        {
            double b=0, denominator=0, numerator=0;
            for (int i = 0; i <year.Length; i++)
            {
                numerator += (year[i] - ayear) * (GDP[i] - aGDP);
                denominator += (year[i] - ayear) * (year[i] - ayear);
            }
            b = numerator / denominator;
            return b;
         }
        //计算a值
        public double geta(double aGDP, double ayear, double b)
        {
            double a = aGDP - ayear * b;
            return a;
        }
	
   
	
	

	}
	

}
	

	

    

